********************************************************************************
* On the Optimal Design of Transfers and Income-Tax Progressivity
* Data: Survey of Consumer Finances
********************************************************************************

clear
clear matrix
macro drop _all
scalar drop _all
clear mata
set more off

********************************************************************************
* Load Data
********************************************************************************

* Use full dataset for 2013
set maxvar 10000
use "Datasets\p13i6.dta", clear

********************************************************************************
* Select variables
********************************************************************************

* Identifiers
rename YY1 yy1
rename Y1 y1

* Income variables

rename X5729 inc_total
label var inc_total "Income from all sources"

rename X5702 inc_wage
label var inc_wage "Income from wages and salaries"

rename X5704 inc_bus
label var inc_bus "Income from sole proprietorship and farm"

replace inc_bus = 0 if inc_bus == -1
replace inc_bus = . if inc_bus == -9 & inc_total < 0

*	Note from bulletin.macro
*   NOTE: Components of income may not sum to INCOME and in the public
*   data X5704/X5714/X5724 may have a value of -9 if X5729 was
*   negative and X5704/X5714/X5724 was also negative

/* For X5704:
ANNUAL $ AMOUNT IN 2012:
	-1.     Nothing
	-2.     Negative value inferred from data on business
	-9.     Other negative value and x5729 negative (public
			data set only)
	 0.     Inap. (no business income: X5703^=1)
*********************************************************
	ORIGINALLY ALLOWED VALUES: [-99999999,...,999999999]

	IF OUT OF RANGE: ILLEGAL VALUE ERROR MESSAGE
*********************************************************
	FOR THE PUBLIC DATA SET, NEGATIVE VALUES INCLUDING
	-2 ARE SET TO -9 WHEN X5729 IS NEGATIVE
*********************************************************
*/

rename X5706 inc_mbd
label var inc_mbd "Income from non-taxable investments such as municipal bonds"

rename X5708 inc_int
label var inc_int "Income from other interest"

rename X5710 inc_div
label var inc_div "Income from dividends"

rename X5712 inc_cap
label var inc_cap "Income from capital gains"

replace inc_cap = 0 if inc_cap == -1

/* For X5712:
ANNUAL $ AMOUNT IN 2012:
	-1.     Nothing
	 0.     Inap. (no capital gains or losses: X5711^=1)
*********************************************************
	ORIGINALLY ALLOWED VALUES: [-99999999,...,999999999]

	IF OUT OF RANGE: ILLEGAL VALUE ERROR MESSAGE
*********************************************************
*/

rename X5714 inc_obu
label var inc_obu "Income from other businesses or investments, net rent, trusts, or royalties"

replace inc_obu = 0 if inc_obu == -1
replace inc_obu = . if inc_obu == -9 & inc_total < 0

/* For X5714:
ANNUAL $ AMOUNT IN 2012:
	-1.     Nothing
	-9.     Negative and X5729 negative (public data set only)
	 0.     Inap. (no rental, trust, or royalty income:
			X5713^=1)
*********************************************************
	ORIGINALLY ALLOWED VALUES: [-99999999,...,999999999]

	IF OUT OF RANGE: ILLEGAL VALUE ERROR MESSAGE
*********************************************************
	FOR THE PUBLIC DATA SET, NEGATIVE VALUES ARE SET TO
	-9 WHEN X5729 IS NEGATIVE
*********************************************************
*/

rename X5716 inc_uib
label var inc_uib "Income from unemployment or worker's compensation"

rename X5718 inc_ali
label var inc_ali "Income from child support or alimony"

rename X5722 inc_ret
label var inc_ret "Income from Social Security or other pensions, annuities, or other disability or retirement programs"

rename X5720 inc_wel
label var inc_wel "Income from TANF, SNAP (food stamps), or other forms of welfare or assistance such as SSI"

rename X5724 inc_oth
label var inc_oth "Income from any other sources"

replace inc_oth = . if inc_oth == -9 & inc_total < 0

/* For X5724:
ANNUAL $ AMOUNT IN 2012:
	-9.     Negative and x5729 negative (public data set only)
	 0.     Inap. (no other income: X5723^=1)
*********************************************************
	ORIGINALLY ALLOWED VALUES:
	   [-99999999,..., -1, 1,...,999999999]

	IF OUT OF RANGE: ILLEGAL VALUE ERROR MESSAGE
*********************************************************
	FOR THE PUBLIC DATA SET, NEGATIVE VALUES ARE SET TO
	-9 WHEN X5729 IS NEGATIVE
*********************************************************
*/

keep yy1 y1 inc_total inc_wage inc_bus inc_mbd inc_int inc_div inc_div inc_cap inc_obu inc_uib inc_ali inc_ret inc_wel inc_oth

********************************************************************************
* Merge summary public dataset
********************************************************************************

* Merge
merge 1:1 yy1 y1 using "Datasets\rscfp2013.dta"

* Recode implicate number (five replicates per individual because of multiple imputation structure)
gen imp = real(substr(string(y1),-1,1))
label var imp "Implicate number"

* Divide weight by 5
* See imputation section of codebook
replace wgt = wgt/5

keep yy1 y1 imp wgt age inc_total inc_wage inc_bus inc_mbd inc_int inc_div inc_div inc_cap inc_obu inc_uib inc_ali inc_ret inc_wel inc_oth networth wageinc

********************************************************************************
* Generate income variables
********************************************************************************

* Labor income
egen inc_lab = rowtotal(inc_wage inc_bus inc_obu inc_uib), missing
label var inc_lab "Labor income (our definition)"

* Compare wage income from full dataset to summary extract dataset
assert (wageinc - 1.114439368247986*inc_wage) <= 1  // inflation adjustment
drop wageinc

* Total income
egen inc_tot = rowtotal(inc_lab inc_mbd inc_int inc_div inc_cap inc_oth), missing
label var inc_tot "Total income (our definition)"

********************************************************************************
* Sample Restrictions
********************************************************************************

* Age restriction
drop if age < 25
drop if age > 60

* Income restriction
drop if inc_lab < 5000
drop if inc_tot < 5000

* Drop if not all implicates satisfy sample selection criteria (implicates 1+2+3+4+5=15)
bysort yy1: gen aux = _n
by yy1: egen aux2 = total(aux)
drop if aux2 != 15
drop aux aux2

********************************************************************************
* Moments of income distributions
********************************************************************************

* Labor income
mean inc_lab [pw = wgt]
_pctile inc_lab [pw = wgt]
return list

* Total income
mean inc_tot [pw = wgt]
_pctile inc_tot [pw = wgt]
return list

********************************************************************************
* Labor income distribution
********************************************************************************

xtile inc_lab_pct = inc_lab [pw = wgt], nq(100)

total inc_lab if inc_lab_pct <= 20 [pw = wgt]
scalar q1_inc_lab = e(b)[1,1]
total inc_lab if inc_lab_pct > 20 & inc_lab_pct <= 40 [pw = wgt]
scalar q2_inc_lab = e(b)[1,1]
total inc_lab if inc_lab_pct > 40 & inc_lab_pct <= 60 [pw = wgt]
scalar q3_inc_lab = e(b)[1,1]
total inc_lab if inc_lab_pct > 60 & inc_lab_pct <= 80 [pw = wgt]
scalar q4_inc_lab = e(b)[1,1]
total inc_lab if inc_lab_pct > 80 [pw = wgt]
scalar q5_inc_lab = e(b)[1,1]
total inc_lab if inc_lab_pct > 90 [pw = wgt]
scalar top10_inc_lab = e(b)[1,1]

total inc_lab [pw = wgt]
scalar all_inc_lab = e(b)[1,1]

scalar q1_inc_lab_share = q1_inc_lab/all_inc_lab
scalar q2_inc_lab_share = q2_inc_lab/all_inc_lab
scalar q3_inc_lab_share = q3_inc_lab/all_inc_lab
scalar q4_inc_lab_share = q4_inc_lab/all_inc_lab
scalar q5_inc_lab_share = q5_inc_lab/all_inc_lab
scalar top10_inc_lab_share = top10_inc_lab/all_inc_lab

scalar drop q1_inc_lab q2_inc_lab q3_inc_lab q4_inc_lab q5_inc_lab top10_inc_lab all_inc_lab
scalar list _all

********************************************************************************
* Networth distribution
********************************************************************************

xtile networth_pct = networth [pw = wgt], nq(100)

total networth if networth_pct <= 20 [pw = wgt]
scalar q1_networth = e(b)[1,1]
total networth if networth_pct > 20 & networth_pct <= 40 [pw = wgt]
scalar q2_networth = e(b)[1,1]
total networth if networth_pct > 40 & networth_pct <= 60 [pw = wgt]
scalar q3_networth = e(b)[1,1]
total networth if networth_pct > 60 & networth_pct <= 80 [pw = wgt]
scalar q4_networth = e(b)[1,1]
total networth if networth_pct > 80 [pw = wgt]
scalar q5_networth = e(b)[1,1]
total networth if networth_pct > 90 [pw = wgt]
scalar top10_networth = e(b)[1,1]

total networth [pw = wgt]
scalar all_networth = e(b)[1,1]

scalar q1_networth_share = q1_networth/all_networth
scalar q2_networth_share = q2_networth/all_networth
scalar q3_networth_share = q3_networth/all_networth
scalar q4_networth_share = q4_networth/all_networth
scalar q5_networth_share = q5_networth/all_networth
scalar top10_networth_share = top10_networth/all_networth

scalar drop q1_networth q2_networth q3_networth q4_networth q5_networth top10_networth all_networth
scalar list _all